package com.lcq.gulimall.cart.web;

import com.lcq.gulimall.cart.service.CartService;
import com.lcq.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.concurrent.ExecutionException;

@Controller
public class CartAddController {
    @Autowired
    CartService cartService;

    @RequestMapping("/success.html")
    public String success(@RequestParam("skuId") Long skuId, Model model){
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItem);
        return "success";
    }

    @RequestMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("count")Integer count, RedirectAttributes ra){
        try {
            cartService.addSku(skuId,count);
            ra.addAttribute("skuId",skuId);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "redirect:http://cart.gulimall.com/success.html";
    }
}
